{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "22a77445",
   "metadata": {},
   "source": [
    "## 响应状态码\n",
    "### response.status_code 查看响应状态码\n",
    ">> 200 请求成功  \n",
    ">> 301 资源或网页永久转移到其他url  \n",
    ">> 404 请求的资源或网页不存在  \n",
    ">> 500 内部服务器错误  \n",
    ">>> response.status_code == requests.codes.ok 帮助人们在编写爬虫时判断是否正确地获取了资源  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "48d5b27e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "from lxml import etree\n",
    "import csv\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "14ef946e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在获取： 中海观园国际 \n",
      "正在获取： 阳光桃源 \n",
      "正在获取： 吉祥花苑 \n",
      "正在获取： 帝景苑 \n",
      "正在获取： 希尔顿阳光 \n",
      "正在获取： 恒安大厦 \n",
      "正在获取： 美林海岸 \n",
      "正在获取： 公园御景 \n",
      "正在获取： 棠德花苑 \n",
      "正在获取： 丽晶华庭 \n",
      "正在获取： 翠湖山庄 \n",
      "正在获取： 翠湖山庄 \n",
      "正在获取： 华康小区 \n",
      "正在获取： 体育东小区 \n",
      "正在获取： 盈彩美居 \n",
      "正在获取： 华骏花园 \n",
      "正在获取： 旭景家园 \n",
      "正在获取： 省府宿舍 \n",
      "正在获取： 金燕花园 \n",
      "正在获取： 翰林苑(天河) \n",
      "正在获取： 天诚广场 \n",
      "正在获取： 华南理工大学宿舍南秀村 \n",
      "正在获取： 金碧华府 \n",
      "正在获取： 新世界东逸花园 \n",
      "正在获取： 嘉宝华庭 \n",
      "正在获取： 金建苑 \n",
      "正在获取： 六运小区 \n",
      "正在获取： 金建苑 \n",
      "正在获取： 电子五所宿舍 \n",
      "正在获取： 保利林海山庄 \n"
     ]
    }
   ],
   "source": [
    "def write_csv(item):\n",
    "    with open('tianhe_second-hand_house.csv','a', encoding='utf-8') as f:\n",
    "        writer = csv.writer(f)\n",
    "        try:\n",
    "            writer.writerow(item)\n",
    "        except:\n",
    "            print('报错啦')\n",
    "            \n",
    "if __name__ == '__main__':\n",
    "    headers = {\n",
    "       \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36\"\n",
    "    }\n",
    "    start_url = 'https://gz.lianjia.com/ershoufang/tianhe/pg'\n",
    "    \n",
    "    for i in range(1,2):\n",
    "        url = start_url + str(i)\n",
    "        html = requests.get(url,headers = headers)\n",
    "        time.sleep(1)\n",
    "        selector = etree.HTML(html.text)\n",
    "        tianhe_list = selector.xpath('//*[@id=\"content\"]/div[1]/ul/li')\n",
    "        for tianhe in tianhe_list:\n",
    "            title = tianhe.xpath('div[1]/div[1]/a/text()')[0]\n",
    "            address = tianhe.xpath('div[1]/div[2]/div/a[1]/text()')[0]\n",
    "            address_detail = tianhe.xpath('div[1]/div[2]/div/a[2]/text()')[0]\n",
    "            house_info = tianhe.xpath('div[1]/div[3]/div/text()')[0]\n",
    "            price = tianhe.xpath('div[1]/div[6]/div[1]/span/text()')[0]\n",
    "            unit_price = tianhe.xpath('div[1]/div[6]/div[2]/span/text()')[0]\n",
    "            item = [title, address, address_detail, house_info, price, unit_price]\n",
    "            write_csv(item)\n",
    "            print('正在获取：',address)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "12b2dab2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "776cb5ba",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c164d21",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e8e87d23",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
